home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!eskimo!news
- From: mag@eskimo.com (mAg)
- Subject: Re: How to access memory allocated in function
- X-Nntp-Posting-Host: tia1.eskimo.com
- Message-ID: <DLD5wM.KK5@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: *.*
- X-Newsreader: WinVN 0.93.10
- References: <4ddbe3$so3@josie.abo.fi>
- Date: Thu, 18 Jan 1996 06:26:46 GMT
-
- In article <4ddbe3$so3@josie.abo.fi> (Mon, 15 Jan 96 13:46:17 GMT), csundqvi@abo.fi
- says :
- >
- >Hello
- >
- >
- >How can i access memory allocated dynamically in a function after i leave the
- >function, something like this:
- >
- >main()
- >{
- >int *p;
- >sub(p);
- >}
- >
- >void sub(int *p)
- >{
- > p = malloc(.....);
- >}
- >
- >Thanks !
-
- There is no way to free that memory unless you do it in the sub itself. Anyway in the
- above example the local copy of 'p' in sub() will hold the result of malloc() and
- forget it as soon as the function terminates.
-
- here is an example how to allocate in a function and use the memory in the caller.
-
- #include <stdlib.h>
-
- void sub(int **pp)
- {
- *pp = malloc(.......);
- }
-
- int main(void)
- {
- int *p;
- sub(&p);
- /* do whatever with block pointed by p */
-
- ...
- ...
- ...
-
- free(p);
- }
-
-
- --
- /* --------------------------------------------------------
- MAG@ESKIMO.COM
- http://www.eskimo.com/~mag/index.html
- ***********************************************************
- To understand recursion one must first understand recursion
- ***********************************************************
- -------------------------------------------------------- */
-
-